package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.service.ProductBomService;
import com.uwlaser.service.dto.ProductBomDto;
import com.uwlaser.service.dto.ProductBomQueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "制造BOM管理")
@RequestMapping("/api/productBom")
public class ProductBomController {

    private final ProductBomService productBomService;

    @GetMapping
    @Log("查询制造BOM")
    @ApiOperation("查询制造BOM")
    @PreAuthorize("@el.check('productBom:list')")
    public ResponseEntity query(ProductBomQueryParam query, Pageable pageable){
        return new ResponseEntity<>(productBomService.queryAll(query,pageable),HttpStatus.OK);
    }

    @GetMapping("/getBomTree")
    @Log("查询Bom树结构")
    @ApiOperation("查询Bom树结构")
    @PreAuthorize("@el.check('productBom:list')")
    public ResponseEntity getBomTree(ProductBomQueryParam query){
        return new ResponseEntity<>(productBomService.getBomTree(query),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增制造BOM")
    @ApiOperation("新增制造BOM")
    @PreAuthorize("@el.check('productBom:add')")
    public ResponseEntity create(@Validated @RequestBody ProductBomDto resources){
        return new ResponseEntity<>(productBomService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改制造BOM")
    @ApiOperation("修改制造BOM")
    @PreAuthorize("@el.check('productBom:edit')")
    public ResponseEntity update(@Validated @RequestBody ProductBomDto resources){
        productBomService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除制造BOM")
    @ApiOperation("删除制造BOM")
    @PreAuthorize("@el.check('productBom:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        productBomService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出制造BOM")
    @ApiOperation("导出制造BOM")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('productBom:list')")
    public void download(HttpServletResponse response, ProductBomQueryParam query) throws IOException {
        productBomService.download(productBomService.queryAll(query), response);
    }*/

}
